package com.shangqing.web.controller.system.geology;

/*import cn.hutool.json.XML;
import com.alibaba.fastjson.JSONObject;*/
import com.shangqing.common.annotation.Log;
import com.shangqing.common.core.controller.BaseController;
import com.shangqing.common.core.domain.AjaxResult;
import com.shangqing.system.service.geology.DisastersTypeService;
import com.shangqing.system.service.geology.GeologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/geology")
public class GeologyController extends BaseController {
    @Autowired
    GeologyService geologyService;
    @Autowired
    DisastersTypeService disastersTypeService;
    @Autowired
    RestTemplate restTemplate;
    @Log(title = "地质灾情信息")
    @PreAuthorize("@ss.hasPermi('geology:list')")
    @PostMapping("/list/{Num}")
    public AjaxResult findGeologyList(@PathVariable(value = "Num",required = false)String Num){

         return AjaxResult.success("成功",geologyService.findGeologyList(Num));
    }
    @Log(title = "全区地质灾情统计")
    @PostMapping("/disasterList/{year}/{quarter}/{geologyTypeList}")
    public AjaxResult findGeologyQuarterList(@PathVariable(value = "year",required = false)String year,
                                             @PathVariable(value = "quarter",required = false)String quarter,
                                             @PathVariable(value = "geologyTypeList",required = false)List<String> geologyTypeList){
        Map map=new HashMap();
        for (String geologyType:geologyTypeList){
            String disastersType= disastersTypeService.findDisastersTypeById(geologyType);
            logger.info("===========灾害类型为============"+disastersType);
            logger.info("=========灾害时间为================"+year);
            logger.info("=========灾害季度为================"+quarter);
           Integer Num= geologyService.findGeologyListByQuarter(year,quarter,geologyType);
           map.put("geologyType",disastersType);
           map.put("geologyNum",Num);
           map.put("geologyYear",year);
           map.put("geologyQuarter",quarter);
        }
        return AjaxResult.success("成功",map);
    }
    @Log(title = "各区地质灾害发生占比")
    @PostMapping("/disasterCount/{year}/{quarter}")
    public AjaxResult findCountGeologyByCity(@PathVariable(value = "year",required = false)String year,
                                             @PathVariable(value = "quarter",required = false)String quarter){
        logger.info("=========参数========"+year+"===="+quarter);
        return AjaxResult.success("成功",geologyService.findCountGeologyByCity(year,quarter));
    }
    @Log(title = "本地实时天气")
    @GetMapping("/disaster/Weather")
    public AjaxResult showWeather(){
        /*高德API接口*/
        String weatherUrl = "https://restapi.amap.com/v3/weather/weatherInfo?city=640106&key=ed8913582dbbc7b27152c793cfac49b7";
        ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
        String weatherBody = weatherEntity.getBody();
        System.out.println(weatherBody);
        return AjaxResult.success("成功",weatherBody);
    }
    @Log(title = "2020各区地质灾情统计")
    @PostMapping("/disaster/RegionalStatistics")
    public AjaxResult regionalStatistics(){
        return AjaxResult.success("成功",geologyService.findRegionalDisasterStatistics());
    }
    @GetMapping("/disaster/weekWeather")
    public AjaxResult weekWeather(){
        /*week*/
        String watherUrl="http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100";
        /*day*/

        ResponseEntity<String> weatherEntity = restTemplate.getForEntity(watherUrl, String.class);
        String weatherBody=weatherEntity.getBody();
        /*JSONObject jsonObject= XML.toJSONObject(weatherBody);
        String result=jsonObject.toString();*/
        System.out.println(weatherBody);
        return  AjaxResult.success("成功",weatherBody);
    }

}
